package com.lsec.springbootinit.controller;

import com.lsec.springbootinit.mapper.UserMapper;
import com.lsec.springbootinit.model.common.CommonResult;
import com.lsec.springbootinit.model.entity.SysUser;
import com.lsec.springbootinit.model.vo.auth.AuthLoginReqVO;
import com.lsec.springbootinit.model.vo.auth.AuthLoginRespVO;
import com.lsec.springbootinit.service.SysLoginService;
import com.lsec.springbootinit.utils.JsonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

import static com.lsec.springbootinit.model.common.CommonResult.*;

/**
 * 登录控制器
 *
 * @author Cherry
 * @date 2023-06-25 15:23
 */

@Validated
@RequiredArgsConstructor
@RestController
@Tag(name = "用户认证")
public class SysLoginController {


    private final SysLoginService loginService;
    private final UserMapper userMapper;

    @PostMapping("/login")
    @Operation(summary = "登陆")
    @PermitAll
    public CommonResult<AuthLoginRespVO> login(@RequestBody AuthLoginReqVO authLoginReqVO){
        return success(loginService.login(authLoginReqVO));
    }

    @PostMapping("/user1/login1")
    @Operation(summary = "登陆")
    @PermitAll
    //@PreAuthorize("hasAuthority('test1')")
    public SysUser logint(String name){
        SysUser sysUser = new SysUser();
        sysUser.setLoginDate(LocalDateTime.now());
        String json = JsonUtils.getBeanToJson(sysUser);
        SysUser jsonToBean = JsonUtils.getJsonToBean(json, SysUser.class);
        jsonToBean.setId(1111111111122111111L);
        return jsonToBean;
    }


    @DeleteMapping("/logout")
    @Operation(summary = "注销")
    public CommonResult<Boolean> logout(){
        return success(loginService.logout());
    }

    public static void main(String[] args) {
        boolean notEmpty = StringUtils.isEmpty("null");
        System.out.println("notEmpty = " + notEmpty);
    }
}

